package com.skiving.cloud.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.admin.entity.SysDictionary;
import com.skiving.cloud.admin.mapper.SysDictionaryMapper;
import com.skiving.cloud.admin.service.SysDictionaryService;
import com.skiving.cloud.common.utils.UuidUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;
import org.noear.solon.data.annotation.Cache;
import org.noear.solon.data.annotation.CacheRemove;

import java.io.Serializable;
import java.util.List;

/**
 *  服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-08
 */
@Component
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary>  implements SysDictionaryService {
    /**
     * 分页查询
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysDictionary> queryByPage(PageParameter<SysDictionary> parameter) {
        Page<SysDictionary> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysDictionary::getId).eq(parameter.getParam().getId());
            }
            if (Utils.isNotBlank(parameter.getParam().getClassCode())) {
                queryWrapper.and(SysDictionary::getClassCode).like(parameter.getParam().getClassCode());
            }
            if (Utils.isNotBlank(parameter.getParam().getClassName())) {
                queryWrapper.and(SysDictionary::getClassName).like(parameter.getParam().getClassName());
            }
            if (Utils.isNotBlank(parameter.getParam().getDataCode())) {
                queryWrapper.and(SysDictionary::getDataCode).like(parameter.getParam().getDataCode());
            }
            if (Utils.isNotBlank(parameter.getParam().getDataName())) {
                queryWrapper.and(SysDictionary::getDataName).like(parameter.getParam().getDataName());
            }
            if (Utils.isNotBlank(parameter.getParam().getRemark())) {
                queryWrapper.and(SysDictionary::getRemark).like(parameter.getParam().getRemark());
            }
            if (Utils.isNotBlank(parameter.getParam().getState())) {
                queryWrapper.and(SysDictionary::getState).eq(parameter.getParam().getState());
            }
            if (Utils.isNotBlank(parameter.getParam().getParentId())) {
                queryWrapper.and(SysDictionary::getParentId).eq(parameter.getParam().getParentId());
            }
        }
        queryWrapper.orderBy(SysDictionary::getClassCode, true)
                .orderBy(SysDictionary::getDataOrder, true);
        return this.page(page, queryWrapper);
    }

    /**
     * 根据字典编号取字典数据
     *
     * @param classCode 字典编码
     * @return 字典项
     */
    @Override
    @Cache(key="skivingDic:queryByClassCode_${classCode}", tags = "skivingDic")
    public List<SysDictionary> queryByClassCode(String classCode) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(SysDictionary::getClassCode).eq(classCode)
                .orderBy(SysDictionary::getDataOrder).asc();
        return this.list(queryWrapper);
    }

    /**
     * 新增数据字典
     *
     * @param sysDictionary 数据字典
     */
    @Override
    @CacheRemove(keys="skivingDic:queryByClassCode_${arg0.classCode}")
    public void addDic(SysDictionary sysDictionary) {
        sysDictionary.setId(UuidUtil.get32UUID());
        this.mapper.insert(sysDictionary);
    }

    /**
     * 修改数据字典
     *
     * @param sysDictionary 数据字典
     */
    @Override
    @CacheRemove(keys="skivingDic:queryByClassCode_${arg0.classCode}")
    public void updateDic(SysDictionary sysDictionary) {
        this.mapper.update(sysDictionary);
    }

    /**
     * 删除数据字典
     *
     * @param id 字典id
     */
    @Override
    @CacheRemove(tags = "skivingDic")
    public void deleteDic(String id) {
        this.mapper.deleteById(id);
    }


    /**
     * 批量删除
     *
     * @param ids 主键
     */
    @Override
    @CacheRemove(tags = "skivingDic")
    public void deleteBatch(List<String> ids) {
        this.mapper.deleteBatchByIds(ids);
    }

    /**
     * 根据主键获取详细信息。含有上级名称
     *
     * @param id 主键
     * @return 字典信息
     */
    @Override
    public SysDictionary getInfoByIdHasParent(Serializable id) {
        SysDictionary sysDictionary = this.getById(id);
        if (Utils.isNotBlank(sysDictionary.getParentId())) {
            SysDictionary parent = this.getById(sysDictionary.getParentId());
            if (parent != null) {
                sysDictionary.setParentName(parent.getClassName() + "/" + parent.getDataName());
            }
        }
        return sysDictionary;
    }

}
